<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>state练习-点击修改元素背景颜色</title>
    <style>
        #box{
            width: 400px;
            height: 300px;
            background: #789;
        }
    </style>
</head>
<body>
    <div id="root"> </div>

    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/lodash.js/4.17.21/lodash.js"></script>
    <script type="text/babel">
        //声明一个类
        class App extends React.Component{
            //声明 state 属性
            state = {
                bg: '#eaf'
            }

            //render 方法
            render(){
                //获取 bg 颜色
                let {bg} = this.state;
                return <div id="box"  style={{background: bg}}  onClick={this.click}></div>
            }

            click = () => {
                let {random} = _;
                this.setState({
                    bg: `rgb(${random(200, 255)},${random(200, 255)},${random(200, 255)})`
                })
            }
        }
        //渲染
        ReactDOM.render(<App/>, document.querySelector('#root'));
    </script>
</body>
</html>